use serde::{Deserialize, Serialize};
use crate::base::biz_error::{BizCode,SUCCESS,FAIL};

#[derive(Serialize, Deserialize, Debug)]
pub struct Res<T> {
    pub code:i32,
    pub msg:String,
    pub data:Option<T>
}

impl <T> Res<T> {
    pub fn new(code: i32, msg: String, data: Option<T>) -> Self {
        Self { code, msg, data }
    }
    pub fn success(t:T)->Self {
        Self{
            code:0,
            msg:String::from("success"),
            data:Some(t),
        }
    }

    pub fn fail(msg:String)->Self {
        Self{
            code:-1,
            msg,
            data:None
        }
    }

    pub fn build(biz_code:BizCode)->Self {
        Self { code: biz_code.code(), msg: biz_code.msg().to_string(), data: None }
    }

    pub fn build_success()->Self {
        Self::build(SUCCESS)
    }

    pub fn build_fail()->Self {
        Self::build(FAIL)
    }
}
